﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class FormBase : Form
    {
        ToolStripMenuItem m_tsMi = null;

        public FormBase()
        {
            InitializeComponent();
        }

        //构造函数用户禁用主控菜单项；为“窗口”菜单添加子菜单项；为子菜单添加功能事件并设置勾选项；设置窗口和子菜单Tag属性
        public FormBase(ToolStripMenuItem tsMi)
        {
            InitializeComponent();
            //m_tsMi存储传入的功能菜单项
            m_tsMi = tsMi;
            if (m_tsMi != null)
            {
                //禁用功能菜单项
                m_tsMi.Enabled = false;
                //获取一级菜单项个数
                if (m_tsMi.OwnerItem.Owner.Items.Count > 0)
                {
                    //m_tsMi.OwnerItem.Owner.Items:一级菜单项容器；获取“窗口”一级菜单项
                    ToolStripMenuItem tsWinMenu = (ToolStripMenuItem)m_tsMi.OwnerItem.Owner.Items[m_tsMi.OwnerItem.Owner.Items.Count - 1];
                    //创建新菜单项
                    ToolStripMenuItem tsMiNew = new ToolStripMenuItem(m_tsMi.Text);
                    //将新建的菜单项加入到“窗口”菜单子项中
                    tsWinMenu.DropDownItems.Add(tsMiNew);
                    //为新建菜单项绑定事件，以便点击财档项时执行tsMiNew_Click函数
                    tsMiNew.Click += new EventHandler(tsMiNew_Click);
                    //将窗口菜单项的所有子项去掉勾
                    foreach (ToolStripMenuItem mi in tsWinMenu.DropDownItems)
                    {
                        mi.Checked = false;
                    }
                    //勾选当前子项
                    tsMiNew.Checked = true;
                    //注意：控件存在Tag属性，该属性为object类型，所以任何对象均可存储在该属性中
                    //将当前窗口存储到新建菜单项的Tag属性中
                    tsMiNew.Tag = this;
                    //将当前菜单存储到窗口的Tag中
                    this.Tag = tsMiNew;

                }
            }
        }

        //本菜单事件负责执行激活关联窗口并勾选相应菜单项
        void tsMiNew_Click(object sender, EventArgs e)
        {
            //sender表示执行事件的对象，判断sender是否是菜单项
            if (sender is ToolStripMenuItem)
            {
                //获取sender的tag属性，该属性与窗口关联
                Form frm = (sender as ToolStripMenuItem).Tag as Form;
                //激活窗口
                frm.Activate();
                frm.WindowState = FormWindowState.Normal;
                //获取“窗口”一级菜单项
                ToolStripMenuItem tsMiWin = (ToolStripMenuItem)(sender as ToolStripMenuItem).OwnerItem;
                //去掉“窗口”菜单项下的所有子项的勾选
                foreach (ToolStripMenuItem mi in tsMiWin.DropDownItems)
                {
                    mi.Checked = false;
                }
                //勾选当前菜单项
                (sender as ToolStripMenuItem).Checked = true;
            }
        }

        //本事件用于启用主控菜单的功能菜单，并删除“窗口”菜单项的相应子项
        private void FormBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            //启动主控菜单的功能菜单
            if (m_tsMi != null)
                m_tsMi.Enabled = true;
            //窗口的Tag中存储有关联的菜单
            if (Tag is ToolStripMenuItem)
            {
                ToolStripMenuItem tsMi = Tag as ToolStripMenuItem;
                //删除“窗口”菜单中与Form关联的菜单项
                ((ToolStripMenuItem)tsMi.OwnerItem).DropDownItems.Remove(tsMi);
            }
        }
    }
}
